<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addBtn"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column label="序号" width="120" prop="index" type="index" :index="indexMethod" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button size="small" type="success" @click="ShowAssignDialog(row.id)">分配权限</el-button>
              <el-button size="small" type="primary" @click="edit(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delBtn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :total="total"

            @current-change="pageChange"
          />
        </el-row>
      </el-card>

    </div>
    <!-- 新增弹框 -->
    <el-dialog :visible="showDialog" :title="roleForm.id ? '编辑角色' : '新增角色'" @close="btnCancel">
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btn">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 权限组件 -->
    <SettingRoles :show-assign-dialog="showAssignDialog" :role-id="roleId" @closeDialog="closeDialog" />
  </div>

</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting'
import SettingRoles from './components/setting-roles'
export default {
  components: {
    SettingRoles
  },
  data() {
    return {
      roleList: [],
      params: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      showDialog: false,

      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      showAssignDialog: false,
      roleId: ''
    }
  },
  mounted() {
    this.getRoleList()
  },
  // 拉取数据
  methods: {
    async getRoleList() {
      const res = await getRoleList(this.params)
      console.log(res)
      this.roleList = res.data.rows
      this.total = res.data.total
    },
    // 分页
    indexMethod(index) {
    //   console.log(index)
      if (this.params.page !== 1) {
        return (this.params.page - 1) * (this.params.pagesize + 1) + index
      } else {
        return index + 1
      }
    },
    // 点击页数切换数据
    pageChange(page) {
      this.params.page = page
      this.getRoleList()
    },
    // 删除
    delBtn(id) {
      this.$confirm('确定删除此角色吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteRole(id)
        this.getRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {

      })
    },
    // 新增
    addBtn() {
      this.showDialog = true
    },
    // 点击确定按钮并进行兜底校验
    btn() {
      this.$refs.roleForm.validate(async(valid) => {
        if (valid) {
          console.log(this.roleForm)
          if (this.roleForm.id) {
            await updateRole(this.roleForm).then(res => {
              this.$message.success('编辑角色成功')
            })
          } else {
            await addRole(this.roleForm)

            this.$message.success('新增角色成功')
          }
          this.btnCancel()
          this.getRoleList()
          //   this.showDialog = false
        }
      })
    },
    // 点击取消关闭弹框，清除校验痕迹
    btnCancel() {
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    // 获取角色详情
    // async hGetRoleDetail(id) {
    //   // 回填操作
    //   console.log(res)
    // },
    // 编辑
    async edit(id) {
      this.showDialog = true
      const res = await getRoleDetail(id)
      console.log(res.data)
      this.roleForm = res.data
      console.log(this.roleForm)
    //   this.hGetRoleDetail(id)
    },
    // 权限弹出框打开
    ShowAssignDialog(id) {
      console.log('ok')
      this.showAssignDialog = true
      this.roleId = id
    },
    closeDialog() {
      this.showAssignDialog = false
    }
  }
}
</script>

<style lang="less" scoped>
</style>
